package com.entities.projectteam.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.protocols.IKVObject;
import com.core.protocols.IServiceLayerDelegate;
import com.core.servicelayer.HibernateServiceLayer;
import com.core.servlets.TaskTrackerServlet;
import com.core.utils.GlobalConstants;
import com.core.utils.ListUtils;
import com.core.utils.RequestParams;
import com.entities.projectteam.requestmapparser.TeamMemberMapParser;
import com.entities.projectteam.servicelayer.TeamMemberListLayer;
import com.model.project.TeamMember;


public class TeamMemberAdd
        extends TaskTrackerServlet
        implements IServiceLayerDelegate {

	private HttpServletResponse postResponse                 = null;
	private Integer             parentProjectId              = null;
	private final int           teamListFetchRequestUserInfo = 101;

	private static final long   serialVersionUID             = 1L;



	@Override
	protected void doGet ( HttpServletRequest request,
	                       HttpServletResponse response )
	        throws ServletException, IOException {

		this.parentProjectId = RequestParams.paramsFromRequest(request).integerAtIndexForKey(0, "id");

		super.doGet(request, response);
	}



	@Override
	protected void doPost ( HttpServletRequest request,
	                        HttpServletResponse response )
	        throws ServletException, IOException {

		this.postResponse = response;

		RequestParams requestParams = RequestParams.paramsFromRequest(request);
		TeamMember template = TeamMemberMapParser.fromMap(requestParams);



		TeamMemberListLayer listLayer = new TeamMemberListLayer();
		listLayer.setLayerDelegate(this);
		listLayer.setUserInfo(Integer.valueOf(teamListFetchRequestUserInfo));
		listLayer.addProjectTeamMember(template, this.parentProjectId);


	}



	@Override
	public <T> void responseResponseItemsList ( final List <T> items,
	                                            final Object request,
	                                            final Exception exception ) {

		HibernateServiceLayer serviceLayer = (HibernateServiceLayer) request;
		Integer userInfo = (Integer) serviceLayer.getUserInfo();
		if ( userInfo == null ) {
			return;
		}


		switch ( userInfo ) {
			case teamListFetchRequestUserInfo: {
				T item = ListUtils.listItemAtIndex(items, 0);

				this.redirectItemIntoOpenServlet(item);
				break;
			}
			default: {
				break;
			}
		}


	}



	private <T> void redirectItemIntoOpenServlet ( T item ) {

		if ( this.postResponse == null ) {
			return;
		}

		String redirection = String.format("%s", GlobalConstants.SERVLET_PROJECTLIST);

		if ( item != null ) {
			if ( item instanceof IKVObject ) {
				IKVObject kvObject = (IKVObject) item;
				String idValue = kvObject.idValue().toString();
				redirection = String.format("%s?%s", GlobalConstants.SERVLET_TEAMMEMBERINFO, idValue);
			}
		}

		try {
			this.postResponse.sendRedirect(redirection);
		} catch ( IOException e ) {
			e.printStackTrace();
		}

	}


}
